//
//  YCHomeImageColView.swift
//  weiBoSwift
//
//  Created by 刘胤辰 on 16/7/25.
//  Copyright © 2016年 it.com. All rights reserved.
//

import UIKit

let margin=5
let cellHW=80

class YCHomeImageColView: UICollectionView {

    //数据源属性
    var imageLists:[YCImageModel]?{
        
        didSet{
            
            let count = imageLists?.count ?? 0
            let rol = count == 4 ? 2 : count >= 3 ? 3 : count
            let row = count == 4 ? 2 : (count - 1)/3 + 1
            
            let colW=rol*cellHW + (rol-1)*margin
            let colH=row*cellHW + (row-1)*margin
            
            self.snp_updateConstraints { (make) -> Void in
                
                make.width.equalTo(colW)
                make.height.equalTo(colH)
            }
            reloadData()
            //print("________________")
        }
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        self.backgroundColor=UIColor.whiteColor()
        //设置数据源
        self.dataSource=self
        //注册cell
        self.registerClass(YCHomeImageCell.self, forCellWithReuseIdentifier: "cellID")
        
        setUI()
    }
    
    func setUI(){
        
        let layout=self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.minimumInteritemSpacing=CGFloat(margin)
        layout.minimumLineSpacing=CGFloat(margin);
        
        layout.itemSize=CGSize(width: cellHW, height: cellHW)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension YCHomeImageColView : UICollectionViewDataSource{
   
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageLists?.count ?? 0
        
        
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell=collectionView.dequeueReusableCellWithReuseIdentifier("cellID", forIndexPath: indexPath) as? YCHomeImageCell
        
        cell?.model=imageLists![indexPath.item]
        
        return cell!
    }
}

class YCHomeImageCell: UICollectionViewCell{
    
    
    // MARK: -- 懒加载控件
    private lazy var image:UIImageView={
        
        let v=UIImageView(imageName: "weibo_placeholder")
        
        v.contentMode = .ScaleAspectFill
        v.layer.masksToBounds=true
        
        return v
    }()
    
    private lazy var gifImage:UIImageView={
        
        let v=UIImageView(imageName: "timeline_image_gif")
        
        return v
        
    }()
    var model : YCImageModel?{
    
        didSet{
            
            image.yc_webImage(model?.thumbnail_pic, imageName: "weibo_placeholder")
            if let git = model?.thumbnail_pic where git.hasSuffix(".gif"){
                
                gifImage.hidden=false
            }else{
                gifImage.hidden=true
            }
        }
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUI()
    }
    
    func setUI(){
        
        contentView.addSubview(image)
        contentView.addSubview(gifImage)
        
        image.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImage.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(contentView)
            make.bottom.equalTo(contentView)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
